/**   
 * @Title: UserCommunicater.java 
 * @Package com.esun.network.communicater 
 * @Description: (用户与服务器交互相类) 
 * @author City Jiangsy@500wan.com   
 * @date 2011-6-2 下午03:38:03  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.network.communicater;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import com.esun.config.BussinessCode;
import com.esun.config.Config;
import com.esun.config.DistributionChannels;
import com.esun.config.Key;
import com.esun.model.UserInfo;
import com.esun.network.ServerCommunicater;
import com.esun.util.log.EDebug;

/**
 * @ClassName: UserCommunicater
 * @Description: (用户与服务器交互相类)
 * @author City Jiangsy@500wan.com
 * @date 2011-6-2 下午03:38:03
 * 
 */
public class UserCommunicater extends ServerCommunicater {

	private String msgServer; // 服务器信息
	private boolean isLogin = false;

	public String getMsgServer() {
		return msgServer;
	}

	public void setMsgServer(String msgServer) {
		this.msgServer = msgServer;
	}

	/**
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @Title: isLoginSucceed
	 * @Description: (用户登录接口 业务编号10003)
	 * @param @param userName 传入用户名
	 * @param @param userPass 传入密码
	 * @param @return isLogin
	 * @return boolean 返回类型
	 * @throws
	 */
	public boolean isLoginSucceed(String userName, String userPass) throws ClientProtocolException, IOException, ParserConfigurationException, SAXException {
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		list.add(mapItem(BussinessCode.PASER_MID, BussinessCode.MID_LOGIN_SENCOND));
		list.add(mapItem(Key.KEY_USERNAME, userName));
		list.add(mapItem(Key.KEY_USERPASS, userPass));
		list.add(mapItem(Key.KEY_CHANNEL, DistributionChannels.QUDAO));
		list.add(mapItem(BussinessCode.PASER_CK, Config.NULLMARK));
		super.getServerResponse(list, true);
		setMsgServer(super.getErrorMSG());
		EDebug.o(this, "登陆业务接口 username= " + userName + " user pwd = " + userPass + " state = " + super.getState());
		if (super.getState() == 100) {
			isLogin = true;
		} else {
			isLogin = false;
		}
		return isLogin;
	}

	public void updataUserInfo() {
		if (isLogin) {
			UserInfo.getUserInfo().setUserSession(super.getResult().get(BussinessCode.PASER_CK));
			UserInfo.getUserInfo().setUserBalance(super.getResult().get(Key.KEY_MONEY));
			UserInfo.getUserInfo().setUserFreeze(super.getResult().get(Key.KEY_FREEZEMONEY));
		} else {
			UserInfo.getUserInfo().setUserSession(Config.NULLMARK);
			UserInfo.getUserInfo().setUserBalance(Config.NULLMARK);
			UserInfo.getUserInfo().setUserFreeze(Config.NULLMARK);
		}
	}

	/**
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @Title: isRegisterSucceed
	 * @Description: (用户注册接口 业务编号10004)
	 * @param @param userName 传入用户名
	 * @param @param userPwd 传入用户密码
	 * @param @param mobileNum 传入用户注册手机号码
	 * @param @return isRegist
	 * @return boolean 返回类型
	 * @throws
	 */
	public boolean isRegisterSucceed(String userName, String userPwd, String mobileNum) throws ClientProtocolException, IOException, ParserConfigurationException, SAXException {
		boolean isRegist = false;
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		list.add(mapItem(BussinessCode.PASER_MID, BussinessCode.MID_REGIST));
		list.add(mapItem(Key.KEY_USERNAME, userName));
		list.add(mapItem(Key.KEY_USERPWD, userPwd));
		list.add(mapItem(Key.KEY_MOBILE_NUM, mobileNum));
		list.add(mapItem(BussinessCode.PASER_CK, Config.NULLMARK));
		list.add(mapItem(Key.KEY_CHANNEL, DistributionChannels.QUDAO));

		// 写入服务器返回信息
		super.getServerResponse(list, true);
		if (super.getState() == 100) {
			isRegist = true;
		} else {
			isRegist = false;
		}
		setMsgServer(super.getErrorMSG());
		return isRegist;
	}

	/**
	 * @Title: mapItem
	 * @Description: (工具类,用于构建AraayList单条内容)
	 * @param @param name 参数名
	 * @param @param value 参数值
	 * @param @return map
	 * @return HashMap<String,String> 返回类型
	 * @throws
	 */
	private HashMap<String, String> mapItem(String name, String value) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(name, value);
		return map;
	}
}
